package com.lkm.market2_0;

import com.lkm.frame.P;
import com.lkm.frame.module.event.ModuleInitEvent;
import com.lkm.frame.otto.LinkBus;
import com.lkm.frame.otto.Observer;
import com.lkm.frame.task2.Task;
import com.lkm.javatool.StringHelp;
import com.lkm.market2_0.entity.ResponEntity;
import com.lkm.market2_0.help.UIP;
import com.lkm.market2_0.m.app_main.AppMainModule;
import com.lkm.market2_0.entity.SteelyardInfo;
import com.lkm.market2_0.m.steelyarddata.SteelyardDataModule;
import com.lkm.market2_0.m.steelyardserver.SteelyardServerModule;
import com.lkm.market2_0.m.steelyardstatus.StatusInfo;
import com.lkm.market2_0.m.steelyardstatus.SteelyardStatusManagerModule;
import com.lkm.market2_0.netapi.Api;

import java.util.Arrays;
import java.util.TimeZone;



public class MainPresenter implements  MainPresenterI{


    public MainPresenter(ExpandConfig config){
        ExpandConfig.instance=config;
    }
    public  void init() {

        
        TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));

        
//        Locale.setDefault(Locale.Category.FORMAT, Locale.CHINESE);

        Api.init();
        Api.host = ExpandConfig.instance.getServerApiHost();
        P.p("===== Api.host"+Api.host);





        Api.initKey("as:t7:01:10:45:10");

        LinkBus.registerReceiver(new Observer<ModuleInitEvent>() {
            @Override
            public Class<ModuleInitEvent> getBusEventClass() {
                return ModuleInitEvent.class;
            }
            @Override
            public void onReceiver(ModuleInitEvent data, Runnable nextRun) {
                try{
                    if(data.extIModuleClass==AppMainModule.class){
                        LinkBus.unregisterReceiver(this);
                        if(data.isSuccess()) {
                            UIP.p("✨✨✨✨服务器启动成功，开始处理台秤✨✨✨✨","");
                        }else{
                            UIP.p("！！！！服务器启动失败。！！！！","");
                        }
                    }
                }finally {
                    nextRun.run();
                }

            }
        });
        
        UIP.p("重新进入页面","");

    }

    @Override
    public void setOnlyPrint(String SteelyardMac) {


//        if(StringHelp.isEmpty(defaultSteelyardMac)||"全部".equals(defaultSteelyardMac)){


    }



    @Override
    public void setDefaultSteelyardMac(String defaultSteelyardMac) {

    }

    @Override
    public String getDefaultSteelyardMac() {
        return "";
    }

    @Override
    public StatusInfo[] getStatusInfos(int startIndex, int length) {
        SteelyardStatusManagerModule steelyardStatusManagerModule = ExpandConfig.instance.getModuleManager().loadModule(SteelyardStatusManagerModule.class);

        if(steelyardStatusManagerModule==null) {

            return null;
        }

        StatusInfo[] statusInfos = steelyardStatusManagerModule.getStatusInfos();

        return Arrays.copyOfRange(statusInfos,startIndex, Math.min(statusInfos.length, startIndex+length));
    }

    @Override
    public int[] getStatusInfoStat() {
        SteelyardStatusManagerModule steelyardStatusManagerModule = ExpandConfig.instance.getModuleManager().loadModule(SteelyardStatusManagerModule.class);

        if(steelyardStatusManagerModule==null) {

            return null;
        }

        StatusInfo[] statusInfos = steelyardStatusManagerModule.getStatusInfos();

        int countZF=0;
        int countOutline=0;
        int countYC=0;

        for (int i = 0; i < statusInfos.length; i++) {
            StatusInfo si = statusInfos[i];
            int status = 1;
            
            if (si.isOnline()) {
                status = 1;
            } else {
                status = 4;
            }
            if (si.isException()) {
                status = 3;
            }

            if(status==1){
                countZF++;
            }else if (status==4){
                countOutline++;
            }else{
                countYC++;
            }
        }
//        ((TextView) findViewById(R.id.tv_tips)).setText(




        return new int[]{statusInfos.length,countZF,countOutline,countYC};
    }


    @Override
    public Task<Void, Void, ResponEntity<Void>> syncStatusToS() {

        SteelyardStatusManagerModule mSteelyardServerModule= ExpandConfig.instance.getModuleManager().loadModule(SteelyardStatusManagerModule.class);







        return mSteelyardServerModule.upStatusToServer();
    }



//        return DataManager.getInstance().updataData();


    @Override
    public void sendRepairing(String[] steelyardMacs) {
        String SteelyardMac="";

        SteelyardServerModule mSteelyardServerModule= ExpandConfig.instance.getModuleManager().loadModule(SteelyardServerModule.class);
        if(mSteelyardServerModule==null){
            return;
        }

        if(StringHelp.isEmpty(SteelyardMac)||"全部".equals(SteelyardMac)){
//            SteelyardStatusManagerModule steelyardStatusManagerModule = Config.instance.getModuleManager().loadModule(SteelyardStatusManagerModule.class);


            for (int i = 0; i <steelyardMacs.length ; i++) {

                mSteelyardServerModule.setSteelyardRepairing(steelyardMacs[i]);
            }
        }else{
            mSteelyardServerModule.setSteelyardRepairing(SteelyardMac);
        }

    }
    @Override
    public void sendOk(String[] steelyardMacs) {
        String SteelyardMac="";

        SteelyardServerModule mSteelyardServerModule= ExpandConfig.instance.getModuleManager().loadModule(SteelyardServerModule.class);
        if(mSteelyardServerModule==null){
            return;
        }

        if(StringHelp.isEmpty(SteelyardMac)||"全部".equals(SteelyardMac)){
//            SteelyardStatusManagerModule steelyardStatusManagerModule = Config.instance.getModuleManager().loadModule(SteelyardStatusManagerModule.class);

            for (int i = 0; i <steelyardMacs.length ; i++) {

                mSteelyardServerModule.setSteelyardOK(steelyardMacs[i]);
            }
        }else{
            mSteelyardServerModule.setSteelyardOK(SteelyardMac);
        }

    }


    @Override
    public void sendPrint(String[] steelyardMacs) {
        String SteelyardMac= "";

        SteelyardServerModule mSteelyardServerModule= ExpandConfig.instance.getModuleManager().loadModule(SteelyardServerModule.class);
        if(mSteelyardServerModule==null){

            return;
        }

        if(StringHelp.isEmpty(SteelyardMac)||"全部".equals(SteelyardMac)){
//            SteelyardStatusManagerModule steelyardStatusManagerModule = Config.instance.getModuleManager().loadModule(SteelyardStatusManagerModule.class);

            for (int i = 0; i <steelyardMacs.length ; i++) {

                SteelyardMac=steelyardMacs[i];
                mSteelyardServerModule.print(SteelyardMac,"芯片码："+SteelyardMac,SteelyardMac);
            }
        }else{
            mSteelyardServerModule.print(SteelyardMac,"芯片码："+SteelyardMac,SteelyardMac);
        }

    }
    @Override
    public void sendPrintTxt(String[] steelyardMacs,String txt) {
        String SteelyardMac= "";

        SteelyardServerModule mSteelyardServerModule= ExpandConfig.instance.getModuleManager().loadModule(SteelyardServerModule.class);
        if(mSteelyardServerModule==null){
            return;
        }

        for (int i = 0; i <steelyardMacs.length ; i++) {
            SteelyardMac=steelyardMacs[i];
            mSteelyardServerModule.print(SteelyardMac,txt,"");
        }
    }

    @Override
    public void sendTitle(String []steelyardMacs) {
        String SteelyardMac="";

        SteelyardServerModule mSteelyardServerModule= ExpandConfig.instance.getModuleManager().loadModule(SteelyardServerModule.class);
        if(mSteelyardServerModule==null){

            return;
        }
        SteelyardDataModule mSteelyardDataModule= ExpandConfig.instance.getModuleManager().loadModule(SteelyardDataModule.class);
        if(mSteelyardDataModule==null){

            return;
        }

        if(StringHelp.isEmpty(SteelyardMac)||"全部".equals(SteelyardMac)){
//            SteelyardStatusManagerModule steelyardStatusManagerModule = Config.instance.getModuleManager().loadModule(SteelyardStatusManagerModule.class);

            for (int i = 0; i <steelyardMacs.length ; i++) {

                SteelyardMac=steelyardMacs[i];

                SteelyardInfo steelyardInfo=mSteelyardDataModule.getSteelyardInfo(SteelyardMac);
                String title=AppDataCache.getInstance().getOrganizationEntity(steelyardInfo.getOrg()).getName();
                String title2=steelyardInfo.getStallEntity().getStallName();
                mSteelyardServerModule.setTitle(SteelyardMac,title,title2);
            }
        }else{
            SteelyardInfo steelyardInfo= mSteelyardDataModule.getSteelyardInfo(SteelyardMac);
            String title=AppDataCache.getInstance().getOrganizationEntity(steelyardInfo.getOrg()).getName();
            String title2=steelyardInfo.getStallEntity().getStallName();
            mSteelyardServerModule.setTitle(SteelyardMac,title,title2);
        }


    }

    @Override
    public void sendGoodsButtonToSteelyardForS(String []steelyardMacs) {
        String SteelyardMac="";

        AppMainModule mAppMainModule= ExpandConfig.instance.getModuleManager().loadModule(AppMainModule.class);

        if(mAppMainModule==null){

            return;
        }

        if(StringHelp.isEmpty(SteelyardMac)||"全部".equals(SteelyardMac)){
//            SteelyardStatusManagerModule steelyardStatusManagerModule = Config.instance.getModuleManager().loadModule(SteelyardStatusManagerModule.class);

            for (int i = 0; i <steelyardMacs.length ; i++) {

                SteelyardMac=steelyardMacs[i];
                mAppMainModule.syncGoodsButtonToSteelyardForS(SteelyardMac);
            }
        }else{
            mAppMainModule.syncGoodsButtonToSteelyardForS(SteelyardMac);
        }


    }


    

//        dialog_wifiset=ViewUIHelp.showAlertDialog(this, R.layout.dialog_wifiset, "", "设置", "取消", new Runnable() {


//                String wifiName=((EditText)dialog_wifiset.findViewById(R.id.tv_wifiname)).getText().toString();
//                String wifiPass=((EditText)dialog_wifiset.findViewById(R.id.tv_wifipass)).getText().toString();
//                String ip=((EditText)dialog_wifiset.findViewById(R.id.tv_wifiip)).getText().toString();
//                String port =((EditText)dialog_wifiset.findViewById(R.id.tv_wifiport)).getText().toString();






//                MainActivity.this.getPreferences(Context.MODE_PRIVATE).edit().putString("p_wifiset",new Gson().toJson(m)).apply();











//        Map<String,String> m=new Gson().fromJson(MainActivity.this.getPreferences(Context.MODE_PRIVATE).getString("p_wifiset","{}"),new com.google.gson.reflect.TypeToken<HashMap<String,String>>() {}.getType());


////        192.168.0.152

//        ((EditText)dialog_wifiset.findViewById(R.id.tv_wifiname)).setText(StringHelp.isEmpty(m.get("wifiName"))?"zhongxing3":m.get("wifiName"));
//        ((EditText)dialog_wifiset.findViewById(R.id.tv_wifipass)).setText(StringHelp.isEmpty(m.get("wifiPass"))?"zx89889842":m.get("wifiPass"));
//        ((EditText)dialog_wifiset.findViewById(R.id.tv_wifiip)).setText(StringHelp.isEmpty(m.get("ip"))?"192.168.0.152":m.get("ip"));
//        ((EditText)dialog_wifiset.findViewById(R.id.tv_wifiport)).setText(StringHelp.isEmpty(m.get("port"))?"47111":m.get("port"));




//        SteelyardServerModule mSteelyardServerModule= Config.instance.getModuleManager().loadModule(SteelyardServerModule.class);





////        +++；AT+WMODE=STA；AT+WSTA=market2.0,ast12345678；AT+UART=115200,1,NONE；AT+SOCK=TCPC,192.168.43.1,47111；


//        if(StringHelp.isEmpty(SteelyardMac)||"全部".equals(SteelyardMac)){
//            SteelyardStatusManagerModule steelyardStatusManagerModule = Config.instance.getModuleManager().loadModule(SteelyardStatusManagerModule.class);

















//        SteelyardServerModule mSteelyardServerModule= Config.instance.getModuleManager().loadModule(SteelyardServerModule.class);










//            goodsButtonInfoTos.add(new GoodsButtonInfoTo("xxx"+i,22.2f));















//        if(StringHelp.isEmpty(SteelyardMac)||"全部".equals(SteelyardMac)){
//            SteelyardStatusManagerModule steelyardStatusManagerModule = Config.instance.getModuleManager().loadModule(SteelyardStatusManagerModule.class);













    @Override
    public void reLoadSteelyardInfo(String[] steelyardMacs) {
        SteelyardDataModule mAppMainModule= ExpandConfig.instance.getModuleManager().loadModule(SteelyardDataModule.class);

        if(mAppMainModule==null){

            return;
        }

        for (int i = 0; i <steelyardMacs.length ; i++) {
            String SteelyardMac=steelyardMacs[i];
            mAppMainModule.LoadSteelyardInfoForS(SteelyardMac);
        }

    }
}
